package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyao.common.web.enums.ResultMsgEnum;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.YResult;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.dao.model.BamuPoSupply;
import com.yunyao.dao.model.BamuReconciliation;
import com.yunyao.dao.model.BamuSysDept;
import com.yunyao.dao.service.bamu.IBamuSysDeptService;
import com.yunyao.common.dto.SupplierDto;
import com.yunyao.framework.dto.posyupply.PoSosyupplySaveDto;
import com.yunyao.framework.service.SupplierService;
import com.yunyao.framework.vo.reconciliation.ReconciliationinfoVO;
import com.yunyao.framework.vo.supplier.SupplierInfoVO;
import com.yunyao.framework.vo.supplier.SupplierSaveDto;
import com.yunyao.framework.vo.supplier.SupplierVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author xingjishuai
 * @date 2024-07-17
 */
@Slf4j
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private IBamuSysDeptService iBamuSysDeptService;


    @Override
    public YResult<PageResult<SupplierVO>> getList(SupplierDto supplierDto) {
        // 构建查询条件
        String orgCategory = supplierDto.getOrgCategory();
        String orgName = supplierDto.getOrgName();
        LambdaQueryWrapper<BamuSysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BamuSysDept::getIsDeleted, DataDeletedStatus.NO.getType());
        wrapper.eq(BamuSysDept::getIsSupplier, 1);
        wrapper.eq(BamuSysDept::getOrgCategory, orgCategory);
        wrapper.like(StringUtils.isNotBlank(orgName), BamuSysDept::getOrgName, orgName);
        // 分页查询
        IPage<BamuSysDept> page = new Page<>(supplierDto.getPageIndex(), supplierDto.getPageSize());
        page = iBamuSysDeptService.page(page, wrapper);

        // 处理查询结果
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return YResult.success(new PageResult<>());
        }

        // 转换为VO对象列表
        List<SupplierVO> voList = supplierDaoToVoList(page.getRecords());

        // 返回分页结果
        return YResult.success(new PageResult<>(voList, page.getCurrent(), page.getSize(), page.getPages(), page.getTotal()));
    }

    @Override
    public YResult<SupplierInfoVO> getByIdInfo(SupplierDto supplierDto) {
        // 提取传入参数中的结算单ID
        String id = supplierDto.getId();

        // 通过ID查询对应的结算单，同时确保查询到的结算单未被删除
        BamuSysDept bamuSysDept =
                iBamuSysDeptService.getById(id);

        // 如果查询不到对应的结算单或结算单已被删除，则返回一个表示没有数据的错误结果
        if (Objects.isNull(bamuSysDept)) {
            return YResult.error(ResultMsgEnum.NO_DATA);
        }

        // 如果查询成功，则将结算单信息转换为特定的值对象，并返回一个包含该信息的成功结果
        return YResult.success(daoToInfoVO(bamuSysDept));
    }

    @Override
    public YResult<String> saveInfo(SupplierSaveDto saveDto) {
        BamuSysDept bamuPoSupply = new BamuSysDept();
        BeanUtils.copyProperties(saveDto,bamuPoSupply);
        iBamuSysDeptService.saveOrUpdate(bamuPoSupply);
        return YResult.success(bamuPoSupply.getId());
    }

    @Override
    public YResult<Boolean> deleteInfo(SupplierDto supplierDto) {
        // 根据ID查询要删除的BamuPoSupply对象
        BamuSysDept bamuReconciliation = iBamuSysDeptService.getById(Long.valueOf(supplierDto.getId()));

        // 如果对象不存在，返回错误信息
        if (bamuReconciliation == null) {
            return YResult.error(ResultMsgEnum.NO_DATA);
        }

        return iBamuSysDeptService.removeById(Long.valueOf(supplierDto.getId())) ? YResult.success(true) : YResult.error(ResultMsgEnum.ERROR);
    }


    private SupplierInfoVO daoToInfoVO(BamuSysDept bamuSysDept) {
        SupplierInfoVO reconciliationinfoVO = new SupplierInfoVO();
        BeanUtils.copyProperties(bamuSysDept, reconciliationinfoVO);
        return reconciliationinfoVO;
    }

    private List<SupplierVO> supplierDaoToVoList(List<BamuSysDept> records) {
        return records.stream().map(this::supplierDaoToVo).toList();
    }

    private SupplierVO supplierDaoToVo(BamuSysDept bamuSysDept) {
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(bamuSysDept, supplierVO);
        return supplierVO;
    }
}
